package de.unistuttgart.iaas.interaction.skype;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import de.unistuttgart.iaas.interaction.skype.util.ConfigFileHandler;
import de.unistuttgart.iaas.interaction.skype.database.MySQLAccess;

import com.skype.*;
public class SendMessage {
	
	public static void sendMessage(String configurationData)
	{
		ConfigFileHandler config = new ConfigFileHandler(configurationData);
		MySQLAccess dao = new MySQLAccess();
		
		String skype_id = "";
		String skype_to = "";
		String skype_from = "";
		String skype_textmessage = "";
		String skype_reply = "";
		String skype_responseTemplate = "";
		String skype_filemessage = "";
		
		
		int i=0;

		try
		{
			Connection conn = dao.connDataBase(config);
					
			//send new text messages, update database table senttextmessage
			Statement statementText = conn.createStatement();
			ResultSet rsText = statementText.executeQuery("select id from skydb."+config.getSkydb_table_sentTextMessage()+" where state='NEW'");
			ArrayList list_new   = new   ArrayList(); 

			while(rsText.next())
			{ 
				list_new.add(rsText.getString(1)); 
				i++;
			}
			statementText.close();
			
			for(int j=0;j<i;j++)
			{
				String lv = (String) list_new.get(j);
				Statement sendStatement1 = conn.createStatement();
				ResultSet sendResultset = sendStatement1.executeQuery("select * from skydb."+config.getSkydb_table_sentTextMessage()+" where id='"+lv+"'");

				ArrayList skype_message = new ArrayList();
				
				if(sendResultset.next())
				{
					for(int k=1; k<=9;k++)
					{
						
						skype_message.add(sendResultset.getString(k));
					}
				}
				
				skype_id = (String)skype_message.get(0);
				skype_to = (String)skype_message.get(1);
				skype_textmessage = (String)skype_message.get(3)+"\n";
				skype_reply = (String)skype_message.get(5);
				skype_responseTemplate = (String)skype_message.get(6);
				
				Chat chat = Skype.getContactList().getFriend(skype_to).chat();
				chat.send(skype_textmessage);	
				Calendar cal = Calendar.getInstance();
				DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  			
				String sentTime = df.format(cal.getTime());
				
				Statement update= conn.createStatement();
				update.executeUpdate("update skydb."+config.getSkydb_table_sentTextMessage()+" set sentDate='"+sentTime+"', state='SENT' where id='"+skype_id+"'");
				update.close();				
			}
			
			//send new file messages, update sentfilemessage
			Statement statementFile = conn.createStatement();
			ResultSet rsFile = statementFile.executeQuery("select id from skydb."+config.getSkydb_table_sentFileMessage()+" where state='NEW'");
			
			while(rsFile.next())
			{ 
				list_new.add(rsFile.getString(1)); 
				i++;
			}
			statementFile.close();
			

			for(int j=0;j<i;j++)
			{
				String lv = (String) list_new.get(j);
				Statement sendStatement2 = conn.createStatement();
				ResultSet sendResultset = sendStatement2.executeQuery("select * from skydb."+config.getSkydb_table_sentFileMessage()+" where id='"+lv+"'");

				ArrayList skype_message = new ArrayList();
				
				if(sendResultset.next())
				{
					for(int k=1; k<=9;k++)
					{
						
						skype_message.add(sendResultset.getString(k));
					}
				}
				
				skype_id = (String)skype_message.get(0);
				skype_to = (String)skype_message.get(1);
				skype_filemessage = (String)skype_message.get(3);
				
				skype_reply = (String)skype_message.get(5);
				skype_responseTemplate = (String)skype_message.get(6);

			}
			
			
		}catch(Exception e)
		{
			
		}
	}
	
	

}
